import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/values/colors.dart';
import 'package:enterprise_v6_app/app/core/values/gaps.dart';
import 'package:enterprise_v6_app/app/core/values/images.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/hidden_danger_investigation_ledger_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/investigation_plan_ledger/investigation_plan_ledger_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

///隐患排查 排查计划台账
class InvestigationPlanLedgerPage extends StatelessWidget {
  InvestigationPlanLedgerPage({super.key}){
    Get.lazyPut(InvestigationPlanLedgerController.new);
  }

  @override
  Widget build(BuildContext context) {
    final InvestigationPlanLedgerController controller = Get.find<InvestigationPlanLedgerController>();
    return Scaffold(
      key: controller.riskRightScaffoldKey,
      body: Container(
        color: AppColors.bg_color_grey,
        padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
        child: Column(
          children: [
            Row(
              children: [
                Expanded(
                  child: _getSearchEnterWidget(controller),
                ),
                AppGaps.hGap8,
                _screen(controller),
                AppGaps.vGap10,
              ],
            ),
            AppGaps.vGap12,
            Expanded(
                child: CustomEasyRefreshList<HiddenDangerInvestigationLedgerRecords>(
              key: controller.globalKey,
              dataLoad: controller.getInstrumentData,
              itemBuilder: (item, index) {
                return CustomTitleChartPage(
                  title: item.planName ?? '',
                  titleRightWidget: _getResult(item.taskStatus),
                  isShowTitleRightWidget: Container(
                    margin: EdgeInsets.only(right: 10.w),
                    width: 52.w,
                    height: 18.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: item.lateNum == 0 ? const Color(0xFF33A9FF) : const Color(0xFFFD6B6B),
                        borderRadius: const BorderRadius.only(
                          topLeft: Radius.circular(10),
                          topRight: Radius.circular(10),
                          bottomRight: Radius.circular(10),
                        )),
                    child: Text(
                      // item.taskStatus==1?'已完成':'未完成',
                      item.lateNum == 0 ? '已完成' : '漏检',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 10.sp,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _rowData('排查类型：', item.inspectionTypeName ?? '空'),
                      AppGaps.vGap12,
                      _rowData('排查部门：', '${item.inspectionDepartmentName ?? '空'}-${item.inspectionPersonnelName ?? '空'}'),
                      AppGaps.vGap12,
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 10.w),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(8),
                          color: const Color(0x1A5C6DFF),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Column(
                              children: [
                                _topTitle('点位', const Color(0xFF495061), '${item.numberOfPoints ?? '0'}', const Color(0xFF5C6DFF)),
                                AppGaps.vGap10,
                                _topTitle('补检', const Color(0xFF495061), '${item.reExaminationNum ?? '0'}', const Color(0xFFFC9900)),
                              ],
                            ),
                            _divider(),
                            Column(
                              children: [
                                _topTitle('任务', const Color(0xFF495061), '${item.taskNum ?? '0'}', const Color(0xFF5C6DFF)),
                                AppGaps.vGap10,
                                _topTitle('漏检', const Color(0xFF495061), '${item.lateNum ?? '0'}', const Color(0xFFFD6B6B)),
                              ],
                            ),
                            _divider(),
                            Column(
                              children: [
                                _topTitle('隐患', const Color(0xFF495061), '${item.numberOfHiddenHazardsNum ?? '0'}', const Color(0xFFFFCE24)),
                                AppGaps.vGap10,
                                _topTitle('', const Color(0xFF495061), '', const Color(0xFF5C6DFF)),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ).click(() {
                  controller.onClickButtonPressed(item);
                });
              },
            )),
          ],
        ),
      ), // 右侧筛选栏
      // endDrawer: CustomEndDrawer(
      //   centerContext: Column(
      //     children: [
      //       DepartmentRegionalOrganizationTreePage(
      //         selectedEntity: controller.selectedEntity,
      //         onCallBack: (DepartmentUserTreeEntity entity) {
      //           controller.selectedEntity = entity;
      //         },
      //       ),
      //     ],
      //   ),
      //   reset: controller.reset,
      //   confirm: controller.confirm,
      // ),
    );
  }

  //搜索框
  Widget _getSearchEnterWidget(InvestigationPlanLedgerController controller) {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入计划名称',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen(InvestigationPlanLedgerController controller) {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _topTitle(String title, Color color, String? value, Color square) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap12,
        Text(
          value ?? '空',
          style: TextStyle(
            fontSize: 16.sp,
            color: square,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  // 	任务状态 1：完成 2：未完成
  Widget _getResult(int? i) {
    switch (i) {
      case 1:
        return _title('已完成', const Color(0xFF33A9FF));
      case 2:
        return _title('未完成', const Color(0xFFFD6B6B));
      case 3:
        return _title('正常', const Color(0xFF00CE78));
      default:
        return _title('已完成', const Color(0xFF33A9FF));
    }
  }

  Widget _title(String title, Color color) {
    return Row(
      children: [
        SvgPicture.asset(AppImages.icon_details_right),
      ],
    );
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
          child: Text(
            value,
            style: TextStyle(
              color: const Color(0xFF495061),
              fontSize: 12.sp,
              fontWeight: FontWeight.w400,
            ),
          ),
        )
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 24.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFFFFFFF),
        Color(0xFFFFFFFF),
      ])),
    );
  }

}
